<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateSystemLogsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('system_logs', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->unsignedBigInteger('user_id')->comment('操作用户ID')->index();
            $table->string('action', 100)->comment('操作类型')->index();
            $table->text('description')->comment('操作描述');
            $table->ipAddress('ip_address')->comment('ip地址')->nullable();
            $table->text('user_agent')->nullable()->comment('用户代理');
            $table->json('request_data')->nullable()->comment('请求数据');
            $table->json('response_data')->nullable()->comment('响应数据');

            $table->timestamp('created_at')->nullable()->index();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->comment('系统日志表');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('system_logs');
    }
}
